package com.hashbee.invite.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hashbee.bo.ReturnData;
import com.hashbee.invite.dao.InviteMapper;
import com.hashbee.invite.domain.Invite;
import com.hashbee.invite.model.InvitePage;
import com.hashbee.invite.model.InviteVo;
import com.hashbee.invite.service.InviteService;
import com.hashbee.util.IdWorker;

@Service("inviteService")
public class InviteServiceImpl implements InviteService {
	
    private static final Logger logger = LoggerFactory.getLogger(InviteServiceImpl.class);
    
	@Autowired
    private InviteMapper inviteMapper;
    
	@Override
	public ReturnData<InvitePage> invitelist(InviteVo inviteVo){
		try {
			if("".equals(inviteVo.getEmail().trim()))
				inviteVo.setEmail(null);
			int count = inviteMapper.querylistcount(inviteVo);
			List<Invite> list = inviteMapper.querylist(inviteVo);
			InvitePage invitePage = new InvitePage();
			invitePage.setCount(count);
			invitePage.setList(list);
			return  new ReturnData<InvitePage>(1, "查询邀请列表", invitePage);
					
							//inviteVo.getEmail(), inviteVo.getPageIndex(), inviteVo.getPageSize()));
		}catch(Exception e) {
			logger.error(e.getMessage());
			return  new ReturnData<InvitePage>(0, "查询邀请列表失败", null);
		}
	}
	
	@Override
	public ReturnData<Invite> saveInvite(Invite invite){
		try {
			invite.setCreated(new Date().getTime()+"");
			invite.setUpdated(new Date().getTime()+"");
			invite.setId(new IdWorker(0,1).nextId()+"");
			invite.setStatus("0");
			invite.setVcode(inviteCode());
			inviteMapper.insert(invite);
			return new ReturnData<Invite>(1, "保存成功", null);
		}catch(Exception e) {
			logger.error(e.getMessage());
			return new ReturnData<Invite>(0, "保存失败", null);
		}
	}
	
	public String inviteCode() {
		String encode = UUID.randomUUID()
				.toString().replaceAll("-", "").toLowerCase();
		return encode.substring(0, 5);
	}
}
